package avicit.im.lantop.basic.archiveclassmanager.action.rest;


import avicit.cbb.techclass.tabledefine.commtempletmanager.action.rest.CommTempletManagerRest;
import avicit.im.lantop.basic.archiveclassmanager.action.service.ArchiveclassmanagerService;
import avicit.im.lantop.basic.archiveclassmanager.domain.LantopArchiveClass;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "ArchiveclassmanagerServiceRest", description = "Archive_classmanager_ServiceRest")
@RequestMapping("/api/avicit/im/lantop/basic/archiveclassmanager/action/rest/ArchiveclassmanagerServiceRest")
public class ArchiveclassmanagerServiceRest {


    private static final Logger LOGGER = LoggerFactory.getLogger(ArchiveclassmanagerServiceRest.class);

    @Resource
    private ArchiveclassmanagerService archiveclassmanagerService;


    /**
     * 通过path拿到pathName及仅部门可见设置
     * @param path
     * @return
     */
    @PostMapping("/getPathNameByPath")
    @ApiOperation(value = "通过path拿到pathName及仅部门可见设置")
    public ResponseMsg<Map<String, Object>> getPathNameByPath(@ApiParam(value = "通过path拿到pathName及仅部门可见设置", name = "path") @RequestBody String path) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        Map<String, Object> pathNameByPath = null;
        try {
            pathNameByPath = archiveclassmanagerService.getPathNameByPath(path);
            responseMsg.setResponseBody(pathNameByPath);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 获得最大节点顺序值
     * @param id
     * @return
     */
    @PostMapping("/getMaxNodeSortNumber")
    @ApiOperation(value = "获得最大节点顺序值")
    public ResponseMsg<Integer> getMaxNodeSortNumber(@ApiParam(value = "获得最大节点顺序值", name = "id") @RequestBody String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        int maxNodeSortNumber = 0;
        try {
            maxNodeSortNumber = archiveclassmanagerService.getMaxNodeSortNumber(id);
            responseMsg.setResponseBody(maxNodeSortNumber);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 更新排序号
     * @param map
     */
    @PostMapping("/updateData")
    @ApiOperation(value = "更新排序号")
    public ResponseMsg updateData(@ApiParam(value = "更新排序号", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            archiveclassmanagerService.updateData(map.get("id").toString(), map.get("nodeSortNumber").toString(), null, null,null);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 通过查询条件,返回查询到的节点路径集合
     * @author 钟一华
     * @param value
     * @return
     * @throws Exception String
     * @throws
     */
    @PostMapping("/getNodePathsByNodeName")
    @ApiOperation(value = "通过查询条件,返回查询到的节点路径集合")
    public ResponseMsg<List<String>> getNodePathsByNodeName(@ApiParam(value = "通过查询条件,返回查询到的节点路径集合", name = "value") @RequestBody String value) {
        ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();
        List<String> nodePathsByNodeName = null;
        try {
            nodePathsByNodeName = archiveclassmanagerService.getNodePathsByNodeName(value);
            responseMsg.setResponseBody(nodePathsByNodeName);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 树查询方法
     * @author 钟一华
     * @param map
     * @return
     * @throws Exception List<TreeNode>
     * @throws
     */
    @PostMapping("/findSearchTreeNode")
    @ApiOperation(value = "树查询方法")
    public ResponseMsg<List<TreeNode>> findSearchTreeNode(@ApiParam(value = "树查询方法", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<TreeNode>> responseMsg = new ResponseMsg<>();
        List<TreeNode> searchTreeNode = null;
        try {
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String id = (String) map.get("id");
            List<String> paths = (List<String>) map.get("paths");
            String value = (String) map.get("value");
            searchTreeNode = archiveclassmanagerService.findSearchTreeNode(orgId, deptId, id, paths, value);
            responseMsg.setResponseBody(searchTreeNode);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 递归获取tree当前节点下数据
     * @author 钟一华
     * @param map
     * @return List<TreeNode>
     * @throws
     */
    @PostMapping("/recurFindTreeNodesByParentId")
    @ApiOperation(value = "递归获取tree当前节点下数据")
    public ResponseMsg<List<TreeNode>> recurFindTreeNodesByParentId(@ApiParam(value = "递归获取tree当前节点下数据", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<TreeNode>> responseMsg = new ResponseMsg<>();
        List<TreeNode> treeNodes = null;
        try {
            String id = (String) map.get("id");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            treeNodes = archiveclassmanagerService.recurFindTreeNodesByParentId(id, orgId, deptId, 3);
            responseMsg.setResponseBody(treeNodes);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    @PostMapping("/delTree")
    @ApiOperation(value = "递归获取tree当前节点下数据")
    public ResponseMsg delTree(@ApiParam(value = "递归获取tree当前节点下数据", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String id = (String) map.get("id");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String sysId = (String) map.get("sysId");
            archiveclassmanagerService.delTree(id, orgId, deptId, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 判断新建节点名称是否已存在
     * @param map
     * @return
     */
    @PostMapping("/checkNodeNameByIntfs")
    @ApiOperation(value = "判断新建节点名称是否已存在")
    public ResponseMsg<Boolean> checkNodeNameByIntfs(@ApiParam(value = "判断新建节点名称是否已存在", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        boolean b = false;
        try {
            String parentId = (String) map.get("parentId");
            String archiveClassNodeName = (String) map.get("archiveClassNodeName");
            b = archiveclassmanagerService.checkNodeNameByIntfs(parentId, archiveClassNodeName);
            responseMsg.setResponseBody(b);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 获得节点路径
     * @param parentId
     * @return
     */
    @PostMapping("/getNodePathByParentId")
    @ApiOperation(value = "获得节点路径")
    public ResponseMsg<String> getNodePathByParentId(@ApiParam(value = "获得节点路径", name = "parentId") @RequestBody String parentId) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String nodePathByParentId = null;
        try {
            nodePathByParentId = archiveclassmanagerService.getNodePathByParentId(parentId);
            responseMsg.setResponseBody(nodePathByParentId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 添加tree节点
     * @param lac
     */
    @PostMapping("/saveTree")
    @ApiOperation(value = "添加tree节点")
    public ResponseMsg saveTree(@ApiParam(value = "添加tree节点", name = "lac") @RequestBody LantopArchiveClass lac) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            archiveclassmanagerService.saveTree(lac);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     *获得向上模板数据id
     * @param parentId
     * @return
     */
    @PostMapping("/getModelData")
    @ApiOperation(value = "获得向上模板数据id")
    public ResponseMsg<List<LantopArchiveClass>> getModelData(@ApiParam(value = "获得向上模板数据id", name = "parentId") @RequestBody String parentId) {
        ResponseMsg<List<LantopArchiveClass>> responseMsg = new ResponseMsg<>();
        List<LantopArchiveClass> modelData = null;
        try {
            modelData = archiveclassmanagerService.getModelData(parentId);
            responseMsg.setResponseBody(modelData);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 根据id获得树节点对应数据
     * @param id
     * @return
     */
    @PostMapping("/getArchiveclassmanagerById")
    @ApiOperation(value = "获得向上模板数据id")
    public ResponseMsg<LantopArchiveClass> getArchiveclassmanagerById(@ApiParam(value = "获得向上模板数据id", name = "id") @RequestBody String id) {
        ResponseMsg<LantopArchiveClass> responseMsg = new ResponseMsg<>();
        LantopArchiveClass archiveclassmanagerById = null;
        try {
            archiveclassmanagerById = archiveclassmanagerService.getArchiveclassmanagerById(id);
            responseMsg.setResponseBody(archiveclassmanagerById);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 复制树节点和节点的子节点
     * @param map
     */
    @PostMapping("/saveCopyTreeData")
    @ApiOperation(value = "复制树节点和节点的子节点")
    public ResponseMsg saveCopyTreeData(@ApiParam(value = "复制树节点和节点的子节点", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String parentId = (String) map.get("parentId");
            String copyId = (String) map.get("copyId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String sysId = (String) map.get("sysId");
            String userId = (String) map.get("userId");
            String ip = (String) map.get("ip");
            archiveclassmanagerService.saveCopyTreeData(parentId, copyId, orgId, deptId, sysId, userId, ip);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 通过parentId获得子节点数据
     * @param map
     * @return
     */
    @PostMapping("/getTreeDataByParentId")
    @ApiOperation(value = "通过parentId获得子节点数据")
    public ResponseMsg<Paging<LantopArchiveClass>> getTreeDataByParentId(@ApiParam(value = "通过parentId获得子节点数据", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Paging<LantopArchiveClass>> responseMsg = new ResponseMsg<>();
        Paging<LantopArchiveClass> treeDataByParentId = null;
        try {
            Paging<LantopArchiveClass> page = (Paging<LantopArchiveClass>) map.get("page");
            String parentId = (String) map.get("parentId");
            treeDataByParentId = archiveclassmanagerService.getTreeDataByParentId(page, parentId);
            responseMsg.setResponseBody(treeDataByParentId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 判断目标节点是否存在重复数据
     * @param map
     * @return
     */
    @PostMapping("/isRepeatNode")
    @ApiOperation(value = "判断目标节点是否存在重复数据")
    public ResponseMsg<Boolean> isRepeatNode(@ApiParam(value = "判断目标节点是否存在重复数据", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        try {
            String copyId = (String) map.get("copyId");
            String parentId = (String) map.get("parentId");
            boolean repeatNode = archiveclassmanagerService.isRepeatNode(copyId, parentId);
            responseMsg.setResponseBody(repeatNode);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 通过节点集合返回数据节点信息
     * @param map
     * @return
     */
    @PostMapping("/getDataNodeInfoByParam")
    @ApiOperation(value = "通过节点集合返回数据节点信息")
    public ResponseMsg<Map<String, Object>> getDataNodeInfoByParam(@ApiParam(value = "通过节点集合返回数据节点信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            Map<String, Object> modelParam = (Map<String, Object>) map.get("modelParam");
            Map<String, Object> dataNodeInfoByParam = archiveclassmanagerService.getDataNodeInfoByParam(modelParam);
            responseMsg.setResponseBody(dataNodeInfoByParam);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

}
